
<%--<%@ page contentType="text/html;charset=UTF-8" %>--%>
<%--<%--%>
<%--    String msg = (String) request.getAttribute("msg");--%>
<%--%>--%>
<%--<!DOCTYPE html>--%>
<%--<html>--%>
<%--<head>--%>
<%--    <meta charset="UTF-8"/>--%>
<%--    <title>码农小站-用户注册</title>--%>
<%--    <link rel="icon" href="./favicon/favicon.ico" type="image/x-icon">--%>
<%--    <link href="https://cdn.bootcdn.net/ajax/libs/bootstrap/5.3.2/css/bootstrap.min.css" rel="stylesheet">--%>
<%--    <link rel="stylesheet" href="https://cdn.bootcdn.net/ajax/libs/bootstrap-icons/1.11.1/font/bootstrap-icons.min.css">--%>
<%--    <script src="https://cdn.bootcdn.net/ajax/libs/bootstrap/5.3.2/js/bootstrap.bundle.min.js"></script>--%>
<%--    <style>--%>
<%--        body { background-color: #f8f9fa; }--%>
<%--        .register-card { max-width: 400px; margin: 80px auto; }--%>
<%--    </style>--%>
<%--</head>--%>
<%--<body>--%>
<%--<div class="card shadow register-card">--%>
<%--    <div class="card-body">--%>
<%--        <h4 class="card-title text-center mb-4">博客注册</h4>--%>
<%--        <% if (msg != null) { %>--%>
<%--        <div class="alert alert-danger text-center"><%= msg %></div>--%>
<%--        <% } %>--%>
<%--        <form action="<%=request.getContextPath()%>/loginServlet?method=register" method="post">--%>
<%--            <div class="mb-3">--%>
<%--                <label class="form-label">用户名</label>--%>
<%--                <input type="text" class="form-control" name="username" required>--%>
<%--            </div>--%>
<%--            <div class="mb-3">--%>
<%--                <label class="form-label">密码</label>--%>
<%--                <input type="password" class="form-control" name="password" required>--%>
<%--            </div>--%>
<%--            <div class="mb-3">--%>
<%--                <label class="form-label">电话</label>--%>
<%--                <input type="text" class="form-control" name="phone" required>--%>
<%--            </div>--%>
<%--            <button type="submit" class="btn btn-success w-100">注册</button>--%>
<%--        </form>--%>
<%--        <p class="text-center mt-3">--%>
<%--            已有账号？<a href="<%=request.getContextPath()%>/login.jsp">立即登录</a>--%>
<%--        </p>--%>
<%--    </div>--%>
<%--</div>--%>
<%--<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/js/bootstrap.bundle.min.js"></script>--%>
<%--</body>--%>
<%--</html>--%>




<%@ page contentType="text/html;charset=UTF-8" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%
    String msg = (String) request.getAttribute("msg");
%>
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8"/>
    <title>码农小站-用户注册</title>
    <link rel="icon" href="./favicon/favicon.ico" type="image/x-icon">
    <link href="https://cdn.bootcdn.net/ajax/libs/bootstrap/5.3.2/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="https://cdn.bootcdn.net/ajax/libs/bootstrap-icons/1.11.1/font/bootstrap-icons.min.css">
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/js/bootstrap.bundle.min.js"></script>
    <style>
        body { background-color: #f8f9fa; }
        .register-card { max-width: 400px; margin: 80px auto; }
    </style>
    <script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
    <script>
        $(document).ready(function() {
            $('#registerForm').submit(function(e) {
                e.preventDefault();
                var username = $('input[name="username"]').val();
                var password = $('input[name="password"]').val();
                var phone = $('input[name="phone"]').val();
                $.ajax({
                    url: '<%=request.getContextPath()%>/loginServlet?method=register',
                    type: 'POST',
                    data: { username: username, password: password, phone: phone },
                    success: function(response) {
                        if (response === 'usernameExists') {
                            alert('用户名已存在，请重新输入');
                        } else {
                            window.location.href = '<%=request.getContextPath()%>/index.jsp';
                        }
                    },
                    error: function() {
                        alert('注册失败，请重试');
                    }
                });
            });
        });
    </script>
</head>
<body>
<div class="card shadow register-card">
    <div class="card-body">
        <h4 class="card-title text-center mb-4">博客注册</h4>
        <% if (msg != null) { %>
        <div class="alert alert-danger text-center"><%= msg %></div>
        <% } %>
        <form id="registerForm">
            <div class="mb-3">
                <label class="form-label">用户名</label>
                <input type="text" class="form-control" name="username" required>
            </div>
            <div class="mb-3">
                <label class="form-label">密码</label>
                <input type="password" class="form-control" name="password" required>
            </div>
            <div class="mb-3">
                <label class="form-label">电话</label>
                <input type="text" class="form-control" name="phone" required>
            </div>
            <button type="submit" class="btn btn-success w-100">注册</button>
        </form>
        <p class="text-center mt-3">
            已有账号？<a href="<%=request.getContextPath()%>/login.jsp">立即登录</a>
        </p>
    </div>
</div>
</body>
</html>